package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址簿相关接口")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;

    /**
     * 新增地址簿
     */
    @ApiOperation("新增地址簿")
    @PostMapping
    public void addAddressBook(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.addAddressBook(dto);
    }

    @ApiOperation("地址簿分页查询")
    @GetMapping("page")
    public PageResult<AddressBookResDTO> addAddressBook(AddressBookPageQueryReqDTO dto) {
        return addressBookService.page(dto);
    }

    @ApiOperation("根据ID查询地址簿信息")
    @GetMapping("{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    })
    public AddressBookResDTO findById(@NotNull(message = "id不能为空")@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.copyProperties(addressBook, AddressBookResDTO.class);
    }


    @ApiOperation("修改地址簿")
    @PutMapping("/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    })
    public void addAddressBook(@RequestBody AddressBookUpsertReqDTO dto,@PathVariable Long id) {
        addressBookService.editAddressBook(dto,id);
    }

    @ApiOperation("地址簿批量删除")
    @PutMapping("/batch")
    public void deleteAddressBook(@RequestParam("ids") List<Long> ids) {
        addressBookService.deleteAddressBook(ids);
    }

    @ApiOperation("设置/取消默认地址")
    @PutMapping("/default")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "flag", value = "是否默认地址：0否，1是", required = true, dataTypeClass = Integer.class)
    })
    public void defaultAddressBook(
            @RequestParam("id") Long id,
            @RequestParam("flag") Integer flag) {
        addressBookService.defaultAddressBook(id,flag);
    }


    @ApiOperation("获取默认地址")
    @PutMapping("/defaultAddress")
    public AddressBookResDTO getDefaultAddressBook() {
        return addressBookService.getDefaultAddressBook();
    }




}
